package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.business.dto.ManageShopSelfCatAddDTO;
import com.yuanfeng.business.dto.WebShopGoodsListQueryDTO;
import com.yuanfeng.business.entity.ShopGoodsCatEntity;
import com.yuanfeng.business.mapper.ShopGoodsCatMapper;
import com.yuanfeng.business.mapper.ShopSelfCatMapper;
import com.yuanfeng.business.service.ShopGoodsCatService;
import com.yuanfeng.commoms.aspect.UserPermissionsResult;
import com.yuanfeng.commoms.aspect.UserService;
import com.yuanfeng.commoms.config.PermissionsConstant;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.base.DealerApplyInfoVO;
import com.yuanfeng.commoms.vo.goods.GdsListQueryResultVO;
import com.yuanfeng.commoms.vo.shop.ManageShopSelfCatMenuVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ShopGoodsCatServiceImpl
 * @Description  店铺商品分类关系表
 * @Author cyp
 * @Date 2023-06-14 15:29:06
 * @Version 1.0
 **/
@Service
public class ShopGoodsCatServiceImpl extends ServiceImpl<ShopGoodsCatMapper, ShopGoodsCatEntity>
        implements ShopGoodsCatService {

    @Resource
    private ShopSelfCatMapper shopSelfCatMapper;
    @Override
    public ResponseResult queryShopGoodsList(WebShopGoodsListQueryDTO param) {
        IPage<GdsListQueryResultVO> page = null;
        try {
            List<GdsListQueryResultVO> goodsList = new ArrayList<>();
            Integer parentId = param.getParentId();

            if(null != parentId && 0 == parentId){
                List<Integer> childrenIds = this.baseMapper.selectChildrenIds(param);
                if(!StringUtils.isEmpty(param.getShopCatId())){
                    if(null != childrenIds && childrenIds.size() > 0){
                        childrenIds.add(Integer.valueOf(param.getShopCatId()));
                    }else{
                        childrenIds = new ArrayList<>();
                        childrenIds.add(Integer.valueOf(param.getShopCatId()));
                    }
                    param.setChildrenIds(childrenIds);
                }
            }else{
                if(!StringUtils.isEmpty(param.getShopCatId())){
                    List childrenIds = new ArrayList<>();
                    childrenIds.add(param.getShopCatId());
                    param.setChildrenIds(childrenIds);
                }
            }
            //将对象转换为map
            Map data = ParseMapUtils.beanToMap(param);
            //分页查询数据
            page = this.baseMapper.selectWebShopGoodsList(new Query<GdsListQueryResultVO>().getPage(data),param);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return ResponseResult.success(PageUtils.getPage(page));
    }

    @Override
    public ResponseResult addShopSelfCat(ManageShopSelfCatAddDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, param.getToken());

        if (!userPermissionsResult.getFlag()) {
            return ResponseResult.fail(10086, "没有权限");
        }
        param.setShopId(userPermissionsResult.getShopId());
        if (param.getShopGoodsCatDisplayorder() == null) {
            param.setShopGoodsCatDisplayorder(0);
        }
        param.setShopGoodsCatStatus(1);
        if (param.getShopGoodsCatId() == null) {
            if (null == param.getParentId()) {
                param.setParentId(0);
            }
            this.baseMapper.insertShopSelfCat(param);
        } else {
            this.baseMapper.updateShopSelfCat(param);
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult queryDealerClassInfo(DealerApplyInfoVO dealerApply) {
        ArrayList<ManageShopSelfCatMenuVO> menuList = new ArrayList<>();
        // 根据供应商店铺id 和 商品commonid 查询商品分类
        List<ManageShopSelfCatMenuVO> catIds = this.baseMapper.queryParentCatId(dealerApply.getSupplierShopId(),dealerApply.getCommonId());
        if(null != catIds && catIds.size() > 0 ){
            //根据catIds获取分类的详细信息
            List<ManageShopSelfCatMenuVO> rootMenus = shopSelfCatMapper.selectByCaId(catIds);
            // 获取多级分类
            if(null != rootMenus && rootMenus.size() > 0){
                for (ManageShopSelfCatMenuVO rootMenu : rootMenus) {
                    if(0 == rootMenu.getParentId()){//父级id是0 存入返回集
                        if(null != menuList && menuList.size() > 0 ){//判断一级分类 返回集中是否 不存在就添加
                            for (ManageShopSelfCatMenuVO manageShopSelfCatMenu : menuList) {
                                if(manageShopSelfCatMenu.getShopGoodsCatId().equals(rootMenu.getShopGoodsCatId())){
                                    break;
                                }else{
                                    menuList.add(rootMenu);// 存入返回集
                                    break;
                                }
                            }
                        }else{
                            menuList.add(rootMenu);// 存入返回集
                        }
                    }else{
                        ManageShopSelfCatMenuVO parMenus = shopSelfCatMapper.queryCatIdByParent(rootMenu.getParentId());//根据父级查询父级分类
                        //判断父级分类是否已经存在于返回集中
                        if(null != menuList && menuList.size() > 0 ){
                            int i = 0;
                            flag:for (ManageShopSelfCatMenuVO manageShopSelfCatMenu : menuList) {
                                i++;
                                if(manageShopSelfCatMenu.getShopGoodsCatId().equals(parMenus.getShopGoodsCatId())){
                                    if (manageShopSelfCatMenu.getChildren() == null){
                                        manageShopSelfCatMenu.setChildren(new ArrayList<ManageShopSelfCatMenuVO>());
                                    }
                                    manageShopSelfCatMenu.getChildren().add(rootMenu);//当前分类的父级已经存在与返回集中 就将当前分类存入 当前分类的父级分类中
                                    break;
                                }else{
                                    if(i == menuList.size()){
                                        //当前分类的父级分类不存在 与返回集 创建list集合 存入返回集
                                        List<ManageShopSelfCatMenuVO> suMenuLsit = new ArrayList<>();
                                        suMenuLsit.add(rootMenu);
                                        parMenus.setChildren(suMenuLsit);
                                        if(null != menuList && menuList.size() > 0 ){//判断一级分类 返回集中是否 不存在就添加
                                            for (ManageShopSelfCatMenuVO manageShopSelfCatMenu2 : menuList) {
                                                if(manageShopSelfCatMenu2.getShopGoodsCatId().equals(rootMenu.getShopGoodsCatId())){
                                                    break;
                                                }else{
                                                    menuList.add(parMenus);// 存入返回集
                                                    break flag;
                                                }
                                            }
                                        }else{
                                            menuList.add(parMenus);// 存入返回集
                                            break flag;
                                        }
                                    }
                                }
                            }
                        }else{
                            //当前分类的父级分类不存在 与返回集 创建list集合 存入返回集
                            List<ManageShopSelfCatMenuVO> suMenuLsit = new ArrayList<>();
                            suMenuLsit.add(rootMenu);
                            parMenus.setChildren(suMenuLsit);
                            if(null != menuList && menuList.size() > 0 ){//判断一级分类 返回集中是否 不存在就添加
                                for (ManageShopSelfCatMenuVO manageShopSelfCatMenu : menuList) {
                                    if(manageShopSelfCatMenu.getShopGoodsCatId().equals(rootMenu.getShopGoodsCatId())){
                                        break;
                                    }else{
                                        menuList.add(parMenus);// 存入返回集
                                        break;
                                    }
                                }
                            }else{
                                menuList.add(parMenus);// 存入返回集
                            }
                        }
                    }
                }
            }
        }
        return ResponseResult.success(menuList);
    }
}
